package com.maimang.platform.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname ListUtil
 * @description <br>列表行转列工具类</br>
 * @date 2019-10-22 18:22
 * @email jintaozhao@qq.com
 */
public class ListUtil {

	public <T> List convert(Class<T> clazz, List<T> list) {
		List<List> result;
		Field[] fields = clazz.getDeclaredFields();
		result = new ArrayList<>(fields.length);
		for (int i = 0; i < fields.length; i++) {
			result.add(new ArrayList());
		}
		for (T t : list) {
			for (int i = 0; i < fields.length; i++) {
				List l = result.get(i);
				Field field = fields[i];
				field.setAccessible(true);
				try {
					l.add(field.get(t));
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

}
